# -*- coding=utf-8 -*-

#获取省的信息：https://db.yaozh.com/hmap
#此代码的目的是为了获取每个省都有多少个医院总数
#此代码使用了代理，以防不能访问
#代理网站：https://www.kuaidaili.com/free/，主要使用西刺代理

'''
上一个版本很容易请求不通（demo_get_province_msg_hospital_count3.py），而且很容易被网站拒绝，所以使用新的链接来请求
https://db.yaozh.com/hmap?name=&grade=全部&address=&type=全部&bedstr=&bedend=&province=北京市
'''

'''
换个思路：先获取每个省的总数，然后根据总数，拿到所有的医院的单个页，然后查看一共有多少个医院
此例子是获得所有省市的名字
并且是用requests来替换（感觉下来requests的效率更好）
'''

import requests
import re
import urllib.parse
import time
import urllib.request

proxies = {
        "http": "http://115.183.11.158:9999",
        "http": "http://113.89.53.140:9999",
        "http":"http://113.200.214.164:9999"

    }

#requests.get("http://example.org", proxies=proxies)


def get_regex_msg(str,regex_pattern):
    return re.compile(regex_pattern).findall(str)

# def request_web_body(url):
#     regex_rule='data-names="province" data-list=([\s\S]*?)data-src="">'
#     #html_body_opener=requests.get(url, verify=False,proxies=proxies)
#     html_body_opener = requests.get(url, verify=False, proxies=proxies)
#     #html_body_source=html_body_opener.decode("unicode-escape")
#     html_body_source = html_body_opener.content.decode("unicode-escape")
#     #print(html_body_source)
#     return get_regex_msg(html_body_source,regex_rule)

def request_web_body(url):
    regex_rule='data-names="province" data-list=([\s\S]*?)data-src="">'
    html_body_opener=urllib.request.urlopen(url).read()
    html_body_source=html_body_opener.decode("unicode-escape")
    #print(html_body_source)
    return get_regex_msg(html_body_source,regex_rule)

def generate_province_list(province_msg):
    pat_province_name = '"name":"([\s\S]*?)","val"'
    province_html_body_list = get_regex_msg(province_msg, pat_province_name)
    #print(province_html_body_list[0])
    province_list=list()
    for i in range(1,len(province_html_body_list)):
        province_list.append(province_html_body_list[i])
    #print(province_list)
    return province_list

def get_province_hospital_count(province_name):
    province_name = urllib.parse.quote(province_name)
    grade = urllib.parse.quote("全部")
    type = urllib.parse.quote("全部")
    base_url = "https://db.yaozh.com/"
    #param = "hmap?grade=%s&p=%d&pageSize=%d&province=%s&type=%s" % (grade, p, pagesize, province_name, type)
    param="hmap?name=&grade=%s&address=&type=%s&bedstr=&bedend=&province=%s" % (grade,type,province_name)
    target_url = base_url + param
    print(target_url)
    time.sleep(5)
    requests.packages.urllib3.disable_warnings()
    headers = {'content-type': 'application/json',
               'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36'}
    page_source_content_info = requests.get(target_url, verify=False,headers=headers, proxies=proxies)
    #print(page_source_content_info.text)
    print(page_source_content_info.status_code)
    return get_province_obj_count(page_source_content_info.content.decode("utf-8"))

def get_province_obj_count(content):
    pattern='data-total="([0-9]+?)"'
    result=re.compile(pattern).findall(content)
    return result[0]

if __name__ == '__main__':
    base_grade = urllib.parse.quote("全部")
    base_type = urllib.parse.quote("全部")
    base_p = 1
    base_pagesize = 30
    base_province = urllib.parse.quote("北京市")
    base_url="https://db.yaozh.com/hmap?name=&grade=%s&address=&type=%s&bedstr=&bedend=&province=%s" % (base_grade,base_type,base_province)
    province_msg=request_web_body(base_url)[0]
    province_list=generate_province_list(province_msg)
    i=0
    for province in province_list:
        print("----------------------------------------------->"+province)
        print(str(i+1))
        i+=1
        province_name = province
        province_count = get_province_hospital_count(province_name)
        print(province_name+":"+province_count)
